clear ;
close all;
BASEDIR = '\\10.12.134.60\linxj\Documents\data\';
FRAME_DATA_PATH = fullfile(BASEDIR, 'frame_data\');
SCRIPT_PATH = fullfile(BASEDIR, 'sleepstg\script\');
% BASEDIR = '/home/linxj/Documents/data/';
% FRAME_DATA_PATH = fullfile(BASEDIR, 'frame_data/');
% SCRIPT_PATH = fullfile(BASEDIR, 'sleepstg/script/');
TYPE_NUM = 5
feature_cell = cell(1,TYPE_NUM);
FEATURE_NUM = 9;

cd(SCRIPT_PATH);
% type maps
type_map = containers.Map;
type_map('w') = 1;
type_map('W') = 1;

type_map('n1') = 2;
type_map('1') = 2;

type_map('n2') = 3;
type_map('2') = 3;

type_map('n3') = 4;
type_map('3') = 4;

type_map('n4') = 4;
type_map('4') = 4

type_map('r') = 5;
type_map('R') = 5;


% feature extract
file1 = fopen('name','w')
cd 'extract_feature/'
    listing = dir(FRAME_DATA_PATH);
    listing_len = length(listing);
    total_feature_array = [];
    total_lable = []
    type_array = zeros(1,TYPE_NUM);
    if listing_len >2
        for n = 3:listing_len
            namesplit = strsplit(listing(n).name, '_');
            type_string = char(namesplit(4));
            type = type_map(type_string)
            feature_f = specturm_feature(FRAME_DATA_PATH, listing(n).name);
            feature_t = specturm_time(FRAME_DATA_PATH, listing(n).name);
            
            feature = [feature_f feature_t]
            type_array(type) = type_array(type) + 1;
            for j = 1: FEATURE_NUM
                feature_cell{type}(j, type_array(type)) = feature(j);
            end 
            total_feature_array = [total_feature_array; feature]
            total_lable = [total_lable; type]
            fprintf(file1, '%s %s %s\n', listing(n).name, type_string, int2str(type_array(type)));
        end
    end
 cd ..;
 fclose(file1)
feature_cell
fileID = fopen('exp.txt','w');
for i = 1:TYPE_NUM
    fprintf(fileID, '%6s \r\n',int2str(i))
    fprintf(fileID,'%6.6f %6.6f %6.6f %6.6f %6.6f %6.6f %6.6f %6.6f\r\n' ,feature_cell{i});
    
end 
fclose(fileID);

% modle = svmtrain(type_array, feature_array,'-s 0 -c 20 -g 0.1')
% svmpredict(pre_type_array, pre_feature_array, modle)
 